import pandas as pd

from graph.JsonWR import read_json_to_groups
from utils.write import write_pkl

def save_graph_data(groups, t):
    # columns = ["group", "count", "mean", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "min", "max"]
    # columns = ["group", "count", "mean", "99.0", "99.1", "99.2", "99.3", "99.4", "99.5", "99.6", "99.7", "99.8", "99.9",
    #            "min", "max"]
    # columns = ["group", "count", "mean", "99.9", "99.91", "99.92", "99.93", "99.94", "99.95", "99.96", "99.97", "99.98", "99.99",
    #            "min", "max"]
    columns = ["group", "count", "mean", "99.9", "99.99", "99.999", "99.9999", "99.99999", "min", "max"]  # csv,pkl文件列属性

    df = pd.DataFrame(columns=columns)

    for key in groups:
        # print(groups.get(key))
        dataDict = {}
        value = groups.get(key)
        intervalLeft = value.__getattribute__("intervalLeft")
        intervalRight = value.__getattribute__("intervalRight")
        # if float(intervalLeft) < 0.05 or float(intervalLeft) > 0.25:
        #     continue
        dataDict["group"] = "%s:%s" % (intervalLeft, intervalRight)
        dataDict["count"] = value.get_count_sum()
        dataDict["mean"] = value.get_global_mean()
        # for i in [round(i, 4) for i in np.arange(99.99, 100, 0.001)]:
        #     dataDict[str(i)] = value.get_percentile(i)
        for i in ["99.9", "99.99", "99.999", "99.9999", "99.99999"]:
            dataDict[i] = value.get_percentile(float(i))
        # print(dataDict, type(dataDict))
        dataDict["min"] = value.get_min()
        dataDict["max"] = value.get_max()
        row = pd.DataFrame(data=dataDict, index=[0])
        df = df.append(row)

    # print(df.info(), type(df))
    write_pkl("data/graph_%s_%s_all.pkl" % ("multi", t), df)
    df.to_csv("data/graph_%s_%s_all.csv" % ("multi", t))


if __name__ == '__main__':
    # 绘图数据准备
    # groupsT = read_json_to_groups("data/group_%s_%s.json" % ("multi", "T"))
    # save_graph_data(groupsT, "T")
    # groupsF = read_json_to_groups("data/group_%s_%s.json" % ("multi", "F"))
    # save_graph_data(groupsF, "F")
    # groupsTF = read_json_to_groups("data/group_%s_%s.json" % ("multi", "TF"))
    # save_graph_data(groupsTF, "TF")

    groupsT = read_json_to_groups("data/group_multi_T_all.json")
    save_graph_data(groupsT, "T")
    groupsF = read_json_to_groups("data/group_multi_F_all.json")
    save_graph_data(groupsF, "F")
    groupsTF = read_json_to_groups("data/group_multi_TF_all.json")
    save_graph_data(groupsTF, "TF")
